package cn.manyufun.myf.ui.news.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import java.util.List;

import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.NewsSummary;
import cn.manyufun.myf.common.commonutils.LogUtils;
import cn.manyufun.myf.common.irecyclerview.universaladapter.ViewHolderHelper;
import cn.manyufun.myf.common.irecyclerview.universaladapter.recyclerview.MultiItemRecycleViewAdapter;
import cn.manyufun.myf.common.irecyclerview.universaladapter.recyclerview.MultiItemTypeSupport;
import cn.manyufun.myf.ui.news.activity.NewsDetailActivity;

/**
 * des:新闻列表适配器
 * <p>
 * on 2016.09.10:49
 */
public class NewsListAdapter extends MultiItemRecycleViewAdapter<NewsSummary> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_PHOTO_ITEM = 1;
    private static final String TAG = "NewsListAdapter";

    public NewsListAdapter(Context context, final List<NewsSummary> datas) {
        super(context, datas, new MultiItemTypeSupport<NewsSummary>() {

            @Override
            public int getLayoutId(int type) {
                return R.layout.item_news;

            }

            @Override
            public int getItemViewType(int position, NewsSummary msg) {
                return TYPE_ITEM;
            }
        });

    }

    //
    @Override
    public void convert(ViewHolderHelper holder, NewsSummary newsSummary) {
        switch (holder.getLayoutId()) {
            case R.layout.item_news:
                setItemValues(holder, newsSummary, getPosition(holder));
                break;
        }
    }

    /**
     * 普通样式
     *
     * @param holder
     * @param newsSummary
     * @param position
     */
    private void setItemValues(final ViewHolderHelper holder, final NewsSummary newsSummary, final int position) {
        String title = newsSummary.getTitle();

        String digest = newsSummary.getBriefInfo();
        String imgSrc = newsSummary.getCoverImageUrl();
        String otherInfo = newsSummary.getLikeNum() + " 赞 / " + newsSummary.getCommentNum() + " 评论 / " + newsSummary.getViewNum() + " 人浏览";
        String authorName = newsSummary.getAuthorName();

        String comicsName = newsSummary.getSubTitle();

        final PopupMenu popupMenu = new PopupMenu(mContext, holder.getView(R.id.img_down));
        popupMenu.inflate(R.menu.notcare);
//        final PopupMenu popup = new PopupMenu(mContext, holder.getView(R.id.spinner_not_care));
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                LogUtils.logd(newsSummary.getAuthorName() + "");
                removeAt(position - 2);
                return false;
            }
        });


        holder.setText(R.id.tv_comics_title, title);
        holder.setText(R.id.news_summary_digest_tv, digest);
        holder.setText(R.id.tv_other_info, otherInfo);
        holder.setText(R.id.tv_author_name, authorName);
        if (!TextUtils.isEmpty(comicsName))
            holder.setText(R.id.tv_comics_name, "来自漫画:" + comicsName);
        holder.setImageRoundCornerUrl(R.id.iv_comics_photo, imgSrc, 10);
        holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putLong(NewsDetailActivity.EXTRA_DATA_ARTICLE_ID, newsSummary.getReferenceId());
                bundle.putString(NewsDetailActivity.EXTRA_DATA_ARTICLE_TITLE, newsSummary.getTitle());
                bundle.putString(NewsDetailActivity.EXTRA_DATA_ARTICLE_IMG, newsSummary.getCoverImageUrl());
                NewsDetailActivity.startAction(mContext, bundle);
            }
        }).setOnClickListener(R.id.img_down, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupMenu.show();
//                spinnerNotCare.performClick();
            }
        });
    }


    @Override
    public int getSize() {
        return mDatas.size();
    }
}
